package com.ruoyi.quality.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.inspectionItem.InspectionItem;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.quality.service.IInspectionItemService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 检测检验项目Controller
 * 
 * @author ruoyi
 * @date 2023-10-17
 */
@RestController
@RequestMapping("/quality/item")
@Api(value="检测检验项",tags = "检测检验项")
public class InspectionItemController extends BaseController
{
    @Autowired
    private IInspectionItemService inspectionItemService;

    /**
     * 查询检测检验项目列表
     */
    @GetMapping("/list")
    @ApiOperation(value="查询检测检验项目列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "页数", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "每页记录数", required = true, dataType = "String"),
    })
    public TableDataInfo list(InspectionItem inspectionItem)
    {
        startPage();
        List<InspectionItem> list = inspectionItemService.selectInspectionItemList(inspectionItem);
        return getDataTable(list);
    }

    /**
     * 获取检测检验项目树形列表
     */
    @GetMapping("/treeselect")
    @ApiOperation(value="获取检测检验项目树形列表")
    public AjaxResult treeselect(InspectionItem inspectionItem)
    {
        List<InspectionItem> inspectionItems = inspectionItemService.selectInspectionItemList(inspectionItem);
        List<InspectionItem> inspectionItemTree = inspectionItemService.buildInspectionItemTree(inspectionItems);
        return AjaxResult.success(inspectionItemTree);
    }

    /**
     * 导出检测检验项目列表
     */
    @Log(title = "检测检验项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InspectionItem inspectionItem)
    {
        List<InspectionItem> list = inspectionItemService.selectInspectionItemList(inspectionItem);
        ExcelUtil<InspectionItem> util = new ExcelUtil<InspectionItem>(InspectionItem.class);
        util.exportExcel(response, list, "检测检验项目数据");
    }

    /**
     * 获取检测检验项目详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation(value="获取检测检验项目详细信息")
    @ApiImplicitParam(paramType="query", name = "id", value = "主键", required = true, dataType = "String")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(inspectionItemService.selectInspectionItemById(id));
    }

    /**
     * 新增检测检验项目
     */
    @Log(title = "检测检验项目", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value="新增检测检验项目")
    public AjaxResult add(@ApiParam(name="检测检验项目",value="传入json格式",required=true)@RequestBody InspectionItem inspectionItem)
    {
        return toAjax(inspectionItemService.insertInspectionItem(inspectionItem));
    }

    /**
     * 修改检测检验项目
     */
    @Log(title = "检测检验项目", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value="修改检测检验项目")
    public AjaxResult edit(@ApiParam(name="检测检验项目",value="传入json格式",required=true)@RequestBody InspectionItem inspectionItem)
    {
        int ret = inspectionItemService.updateInspectionItem(inspectionItem);
        if(ret > 0) {
            return toAjax(ret);
        }
        return AjaxResult.error("当前菜单存在子集无法修改");
    }

    /**
     * 删除检测检验项目
     */
    @Log(title = "检测检验项目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation(value="删除检测检验项目")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        int ret = inspectionItemService.deleteInspectionItemByIds(ids);
        if(ret > 0) {
            return toAjax(ret);
        }
        return AjaxResult.error("当前菜单存在子集无法删除");
    }
}
